package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CompanyForumTypeAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CompanyForumType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 论坛分类
 *
 * @author wu
 */
public class CompanyForumTypeListFragment extends BaseListFragment {

  private CompanyForumTypeAdapter mAdapter;
  private String mMemberCode;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.companyForum);
    mMemberCode = mCurrentUser.getMemberCode();

  }

  /**
   * 设置ListView分割线
   */
  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //int position = mListview.getChildCount();
    //noinspection deprecation
    //mListview.setDivider(new ColorDrawable(getResources().getColor(color.darker_gray)));
    //mListview.setPadding(0, 20, 0, 0);
    //mListview.setDividerHeight(20);

    View header =
        getLayoutInflater(savedInstanceState).inflate(R.layout.header_forum_type_mine, null, false);
    TextView mMyPublishForum = (TextView) header.findViewById(R.id.tv_myPublish_forum);
    TextView mMyAnswerForum = (TextView) header.findViewById(R.id.tv_myAnswer_forum);

    mMyPublishForum.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        //Intent intent = new Intent(getActivity(), CompanyForumListActivity.class);
        //intent.putExtra(CompanyForumListFragment.EXTRA_MINE,CompanyForumListFragment.EXTRA_MY_PUBLIC);
        //startActivity(intent);
        CompanyForumType myPublish = new CompanyForumType();
        myPublish.setName("我发布的话题");
        myPublish.setPublisher(true);
        Intent intent = new Intent(getActivity(), CompanyForumListActivity.class);

        intent.putExtra(CompanyForumListFragment.EXTRA_VALUE, myPublish);
        startActivity(intent);
      }
    });
    mMyAnswerForum.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {

        Intent intent = new Intent(getActivity(), CompanyForumMyAnswerListActivity.class);
        startActivity(intent);
      }
    });
    mListview.addHeaderView(header);
  }

  @Override public String getUrl() {
    return ApiConst.URL_DATA_DICTIONARY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> mParams = new HashMap<>();
    mParams.put("memCode", mMemberCode);
    mParams.put("dictFlg", "topicKindData");
    return mParams;
  }

  @Override public void resolveResponse(String arg0) {
    Type listType = new TypeToken<ArrayList<CompanyForumType>>() {
    }.getType();
    List<CompanyForumType> mForumType =
        new Gson().<ArrayList<CompanyForumType>>fromJson(arg0, listType);
    setAdapter(mForumType);
  }

  private void setAdapter(List<CompanyForumType> forumType) {
    if (forumType == null) {
      forumType = new ArrayList<>();
    }
    mAdapter = new CompanyForumTypeAdapter(getActivity(), forumType);
    setListAdapter(mAdapter);
    ((CompanyForumTypeAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    //避免点击header时的getitem出现-1的情况.
    if (position < mListview.getHeaderViewsCount()) return;
    CompanyForumType mCompanyForumType =
        mAdapter.getItem(position - mListview.getHeaderViewsCount());
    Intent intent = new Intent(getActivity(), CompanyForumListActivity.class);
    intent.putExtra(CompanyForumListFragment.EXTRA_VALUE, mCompanyForumType);
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
